$(document).ready(function() {  
	
	$('#mainTable').on('click', '#addNote', function() {
		$(this).parent().find('#noteDiv').slideToggle();
	});
	
	$('#mainTable').on('click', '#saveNote', function() {
		var $this = $(this);
		var text = $('#noteArea').val();
    	$.get("/RepairService/saveNote", "note=" + text + "&service_order_fk=" + $this.data('service_order'), function(result) {
    		if(result.trim() == "ok") {
    			$this.parent().parent().html("<span>" + text + "</span><input type=\"button\" id=\"editNote\" value=\"Edit note\"/><input type=\"button\" id=\"saveChangesNote\" data-service_order=\"" + $this.data('service_order') + "\" value=\"Save changes\" style=\"display: none;\"/>");
    			apprise('Note successfully added!!!');
    		} else {
    			apprise('Connection problem!!!<br>Please, try again!!!');
    		}

    	});
	});
	
	
	$('#mainTable').on('click', '#editNote', function() {
		$(this).attr('disabled', 'disabled');
		$(this).parent().find('#saveChangesNote').show();
		var $noteSpan = $(this).parent().find("span");
		var text = $noteSpan.text();
		$noteSpan.html('<input type="text" id="noteInput" value="' + text +'"/>');
	});
	
	$('#mainTable').on('click', '#saveChangesNote', function() {
		var $noteSpan = $(this).parent().find("span");
		var text = $noteSpan.find('input').val();
		$(this).hide();
		$noteSpan.parent().find('#editNote').removeAttr('disabled');
		$noteSpan.html(text);
    	$.get("/RepairService/saveNote", "note=" + text + "&service_order_fk=" + $(this).data('service_order'), function(result) {
			if(result.trim() == "ok") {
				apprise('Note successfully edited!!!');
			} else {
				apprise('Connection problem!!!<br>Please, try again!!!');
			}
		});

	});	

});
